﻿using Microsoft.Practices.Prism.Commands;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HDFSViewer.Component;
using HDFSViewer.Model;

namespace HDFSViewer.ViewModel
{
    public class ConnectHadoopWindowVM : NotifyObject
    {
        private string _connectionString = "192.168.8.100:50070";
        public string ConnectionString
        {
            get { return _connectionString; }
            set
            {
                _connectionString = value;
                OK.RaiseCanExecuteChanged();
                this.RaisePropertyChanged("ConnectionString");
            }
        }

        private string _userName = "hadoop";
        public string UserName
        {
            get { return _userName; }
            set
            {
                _userName = value;
                OK.RaiseCanExecuteChanged();
                this.RaisePropertyChanged("UserName");
            }
        }

        public DelegateCommand OK { get; set; }

        public ConnectHadoopWindowVM()
        {
            OK = new DelegateCommand(DoOK, DoCanOK);
        }

        private void DoOK() { }

        private bool DoCanOK()
        {
            string[] hostandport = ConnectionString.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            int port = 0;
            if (hostandport.Length == 2 && int.TryParse(hostandport[1], out port) && port > 0 && !string.IsNullOrWhiteSpace(UserName))
            {
                return true;
            }
            else return false;
        }
    }
}
